package com.kdtech.analyse.app;
import java.util.HashMap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;

public class McctvAppAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://m.cctv.com/dc/n/index.shtml[?]articalID=[A-Z0-9]*.*",
		"http://m.cntv.cn/dc/n/index.shtml\\?articalID=[A-Z0-9]*.*",

	};

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	public static HashMap<String, String> getLinks(String url, String html) {
//		System.out.println(html);
		HashMap<String, String> ret = new HashMap<String, String>();
		String pageId = StringUtils.substringBetween(html, "pageIdELMT","',");
		if(pageId!=null) pageId = StringUtils.substringAfter(pageId, "= '");
//		System.out.println(pageId);
		String jsonUrl = "http://api.cntv.cn/list/kuaiwenmorelist?page_id="+pageId+"&type=zhengwenye&p=1&n=20&serviceId=shoujiyangshiwang&cb=aa&_="+System.currentTimeMillis()+"&cb=jsonp2";
//		String json = CrawlHTML.GetHtml(jsonUrl);
		String json = null;
		if(json!=null) json = json.replace("jsonp2(", "");
//		System.out.println(json);
		if(json!=null) json = StringUtils.substringBeforeLast(json, ");");
		if(json!=null){
			JSONObject obj = JSONObject.parseObject(json);
			JSONArray jsonArray = obj.getJSONArray("itemList");
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject listItem = (JSONObject) jsonArray.get(i);
				String listUrl = listItem.getString("detailUrl");
				String listTitle = listItem.getString("itemTitle");
				ret.put(listUrl, listTitle);
			}

		}
//		System.out.println(json);
		return ret;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;

		String articalID= StringUtils.substringBetween(url, "articalID=","&");
		String jsUrl = "http://api.cntv.cn/apicommon/index?path=iphoneInterface/general/getNextArticleInfo.jsonp&&article_id="+articalID+"&serviceId=shoujiyangshiwang&callback=faun&_="+System.currentTimeMillis()+"&callback=jsonp1";
//		String json = CrawlHTML.GetHtml(jsUrl);
		String json = null;
//		System.out.println(json);
			if(json!=null) json = StringUtils.substringAfter(json, "jsonp1(");
			if(json!=null) json = StringUtils.substringBeforeLast(json,  ")");
			if(json!=null){
				JSONObject obj = JSONObject.parseObject(json);
				title = obj.getString("article_title");
				content = obj.getString("article_content");
				date = DateUtils.matchDate(obj.getString("article_focus_date"));
				author = obj.getString("article_editor_name");
			}
			if(title==null){
				Document doc=Jsoup.parse(htmltxt);
				title =doc.select("div.title_area h2").text();
				date=DateUtils.matchDate(doc.select("p.info").text());
				content =HtmlCleaner.getContentHtml(url, doc.select("div.text_area"));
				author=JSoupUtils.matchAuthor(doc, "来源：");
			}
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}

	

	

	


}

